<?php

namespace app\admin\model\order;

use app\admin\model\goods\Brand;
use app\admin\model\goods\Goods;
use think\Model;


class OrderGoods extends Model
{
    // 表名
    protected $name = 'order_goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    protected $append = [];


    public function goods(){
        return $this->hasOne(Goods::class, 'id', 'goods_id');
    }

    public function brand(){
        return $this->hasOneThrough(Brand::class, Goods::class, 'id', 'id', 'goods_id', 'goods_brand_id');
    }

    public function suborderNote(){
        return $this->hasMany(SuborderNote::class, 'suborder_id', 'id');
    }

    // 判断是否拥有下级分类
    public function hasSubOrderGoods() {
        return $this->hasOne(OrderGoods::class, 'pid', 'id');
    }

    /**
     * 获取子订单的总价
     */
    public function getTotalPriceTextAttr($value, $data){
        return number_format($data['total_price']);
    }

    /**
     * 获取商品的采购价格
     */
    public function getSupplyPriceTextAttr($value, $data){
        return number_format($data['supply_price']);
    }

    /**
     * 获取商品的原价
     */
    public function getPriceTextAttr($value, $data){
        return number_format($data['price']);
    }

    /**
     * 获取客户的采购价格
     */
    public function getPurchasePriceTextAttr($value, $data){
        return number_format($data['purchase_price']);
    }

    /**
     * 获取订单的出货状态
     */
    public function getStatusTextAttr($value, $data) {
        $status = [
            -1 => '取消订单',
            0 => '准备下单',
            1 => '已下单',
            2 => '现货',
            3 => '付款预定',
            4 => '口头预定',
            5 => '入库',
            6 => '韩国已发货',
            7 => '待定',
            '8-1' => '退款 - 延迟',
            '8-2' => '退款 - 断货',
            '8-3' => '退款 - 特殊',
            '9-1' => '换货 - 颜色',
            '9-2' => '换货 - 尺码',
            '9-3' => '换货 - 瑕疵',
            '9-4' => '换货 - 款式',
            10 => '已提货',
        ];
        return $status[$data['status']];
    }

    public function orders(){
        return $this->hasOne(Order::class, 'id', 'order_id')->field('user_id');
    }


}